<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<!-- <cl-pagination /> -->
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert">
			<template #slot-parentId="{ scope }">
				<cl-recipe-select v-model="scope.parentId" :type="scope.type" />
			</template>
		</cl-upsert>
	</cl-crud>
</template>

<script lang="ts" name=" broadcast-recipe" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { deepTree } from "/@/cool/utils";
import { useCool } from "/@/cool";

const { service } = useCool();

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: "配方名称",
			prop: "name",
			component: { name: "el-input", props: { clearable: true } },
		},
		{
			label: "所属分类",
			prop: "parentId",
			hook: {
				submit(value) {
					return value === "" ? null : value;
				}
			},
			component: {
				name: "slot-parentId"
			}
		},
		{
			prop: "topping",
			label: "置顶",
			value: 0,
			flex: false,
			component: { name: "cl-switch" },
			required: true
		},
		{
			label: "象数",
			prop: "numeral",
			hook: "splitJoin",
			component: {
				name: "el-select",
				props: {
					clearable: true, multiple: true,
					reserveKeyword: false,
					filterable: true,
					allowCreate: true
				}
			},
			hidden({ scope }) {
				return !scope.parentId;
			}
		},
		{
			label: "配方关键词",
			prop: "keywords",
			hook: "splitJoin",
			component: {
				name: "el-select",
				props: {
					clearable: true, multiple: true,
					reserveKeyword: false,
					filterable: true,
					allowCreate: true
				}
			},
			hidden({ scope }) {
				return !scope.parentId;
			}
		},
		{
			label: "描述",
			prop: "description",
			component: {
				name: "cl-editor",
				props: {
					name: "cl-editor-wang"
				}
			}
		},
	],
	// onSubmit(data, { next }) {
	// 	data.description = JSON.stringify(data.description)
	// 	next(data);
	// }
	// async onOpen() {
	// 	// 设置标签列表
	// 	service.broadcast.recipe.list().then((res) => {
	// 		Upsert.value?.setOptions(
	// 			"parentId",
	// 			deepTree(res.map((e) => {
	// 				return {
	// 					label: e.name || "",
	// 					value: e.id + ""
	// 				};
	// 			}))
	// 		);
	// 	});
	// }
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "名称", prop: "name", minWidth: 200 },
		{ label: "所含象数", prop: "numeral", minWidth: 140 },
		{ label: "描述", prop: "description", component: { name: "cl-editor-preview", props: { name: "wang" } }, minWidth: 200 },
		{ label: "置顶", prop: "topping", component: { name: "cl-switch" } },
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 160,
			component: { name: "cl-date-text" },
			sortable: true
		},
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 160,
			component: { name: "cl-date-text" },
			sortable: true
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});
const Crud = useCrud(
	{
		service: service.broadcast.recipe,
		onRefresh(_, { render }) {
			service.broadcast.recipe.list({ order: 'topping' }).then((list) => {
				render(deepTree(list));
			});
		}
	},
	(app) => {
		app.refresh();
	}
);
</script>
